package com.example.libowu.musicplayer;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by libowu on 17-12-11.
 */

public class mMyserveForSetTime extends Service implements Runnable{
    private boolean isStart=true;
    private long time;
    private Time exit=new Time();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return exit;
    }

    @Override
    public void onCreate() {
        Log.e("服务启动","服务启动中");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        isStart=false;
        Log.e("服务已结束","the serve is finish");
        super.onDestroy();
    }

    @Override
    public void run() {
        while (isStart){
            --time;
            if(time==0){
                Log.e("时间到","退出软件");
                Intent intent=new Intent();
                intent.setAction("TIME_UP");
                sendBroadcast(intent);
                isStart=false;
                stopSelf();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.e("剩余时间为:",""+time);
        }
    }

    class Time extends Binder{
        public void setTime(long time){
            mMyserveForSetTime.this.time=time;
            Log.e("bine","bine");
        }
        public void setIsStart(boolean isStart){
            mMyserveForSetTime.this.isStart=isStart;
            Log.e("bine","bine");
        }
        public void startTime(){
            new Thread(mMyserveForSetTime.this).start();
            Log.e("bine","bine");
        }
    }
}
